package com.cyw.common.constants;

import java.util.Objects;

/**
 * @Author: cyw
 * @Date: 2021/1/8
 * @Desc: 状态码对象
 * 标识异常来源，异常类型，异常信息等
 */
public class StatusCode {

    public final static String UNKNOWN_ERROR = "未定义错误";

    /**
     * 状态码, 唯一标识
     * TODO 待定 code分配规则：领域标识（2位16进制数字, 需申请）-功能标识（领域内定义）具体错误标识（3位数字，000~999递增），如：A0-01001，FF-20100
     */
    private String code;

    /**
     * 开发友好的错误提示
     */
    private String errorTip;

    /**
     * 用户友好的提示信息
     */
    private String userTip;

    /**
     * 未定义错误提示信息
     * @param code
     */
    public StatusCode(String code) {
        this.code = code;
        if (isSuccess()) {
            this.errorTip = BaseStatusCode.SUCCESS.errorTip;
            this.userTip = BaseStatusCode.SUCCESS.userTip;
        } else {
            this.errorTip = UNKNOWN_ERROR;
            this.userTip = UNKNOWN_ERROR;
        }
    }

    /**
     * 错误提示和用户错误提示相同
     * @param code
     * @param msg
     */
    public StatusCode(String code, String msg) {
        this.code = code;
        this.errorTip = msg;
        this.userTip = msg;
    }

    /**
     * 自定义errorTip和userTip
     * @param code
     * @param errorTip
     * @param userTip
     */
    public StatusCode(String code, String errorTip, String userTip) {
        this.code = code;
        this.errorTip = errorTip;
        this.userTip = userTip;
    }

    public String getCode() {
        return code;
    }

    public String getErrorTip() {
        return errorTip;
    }

    public String getUserTip() {
        return userTip;
    }

    /**
     * 是否成功的状态码
     * @return
     */
    public boolean isSuccess() {
        return BaseStatusCode.SUCCESS.getCode().equals(code);
    }

    /**
     * 是否失败的状态码
     * @return
     */
    public boolean isFail() {
        return !isSuccess();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        StatusCode statusCode = (StatusCode) o;
        return code.equals(statusCode.code);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code);
    }
}
